<!--{template /module/admin/view/ilinei_frame_tag}-->

<form id="frm_grid" name="frmGrid" action="{$ADMIN_SCRIPT}/admin/file/_ilinei&frame=content&do=design&file={$_var[gp_file]}&path={$_var[gp_path]}" method="post">
    <input type="hidden" name="formsubmit" value="yes" />
</form>

<link href="static/css/parser.css?v={$setting[Version]}" rel="stylesheet" type="text/css"/>

<script type="text/javascript" src="static/js/jquery.js?v={$setting[Version]}"></script>
<script type="text/javascript" src="static/js/jquery.parser.js?v={$setting[Version]}"></script>

<script>
    //TAG序号缓存
    var tag_loops = new Array();
    tag_loops['body_prev'] = 0;
    tag_loops['body_next'] = 0;

    //当前页面类型
    var page_type = '{$GLOBALS[_ILINEI_TYPE]}';
    var messages = new Array();

    //内容可选类型按钮
    messages['type'] = '';
    messages['type'] += '<div class="-ilinei-type">';
    messages['type'] += '<p><label><input type="radio" name="rdoType" value="prev" checked="checked"/> {lang admin.ilinei.frame_parser.view.tag.radio.0}</label></p>';
    messages['type'] += '<p><label><input type="radio" name="rdoType" value="next"/> {lang admin.ilinei.frame_parser.view.tag.radio.1}</label></p>';
    messages['type'] += '</div>';

    //内容可选类型按钮
    messages['operate'] = '';
    messages['operate'] += '<p class="-ilinei-operate">';
    messages['operate'] += '<span class="out"><button type="button" class="btn-add-prev"></button>';
    messages['operate'] += '<button type="button" class="btn-add-next"></button>';
    messages['operate'] += '</span><span class="in">';
    messages['operate'] += '<button type="button" class="btn-add-interval-prev"></button>';
    messages['operate'] += '<button type="button" class="btn-add-interval-next"></button>';
    messages['operate'] += '</span>';
    messages['operate'] += '<button type="button" class="btn-delete"></button>';
    messages['operate'] += '</p>';

    var tag_names = new Array();

    tag_names['blank'] = '{lang admin.ilinei.frame_parser.view.button.blank}';
    tag_names['block'] = '{lang admin.ilinei.frame_parser.view.button.block}';
    tag_names['cms_model__category_multi'] = '{lang admin.ilinei.frame_parser.view.button.category.multi}';
    tag_names['cms_model__category_crumbs'] = '{lang admin.ilinei.frame_parser.view.button.category.crumbs}';
    tag_names['cms_model__category_one'] = '{lang admin.ilinei.frame_parser.view.button.category.one}';
    tag_names['cms_model__article_multi'] = '{lang admin.ilinei.frame_parser.view.button.article.multi}';
    tag_names['cms_model__article_one'] = '{lang admin.ilinei.frame_parser.view.button.article.one}';
    tag_names['ad_model__adlog'] = '{lang admin.ilinei.frame_parser.view.button.ad}';

    //顶部保存TAG
    function save(){
        $.dialog({message:'<p class="-ilinei-tips">{lang admin.ilinei.frame_parser.view.submit.message}</p>', button:{cancel:'{lang admin.ilinei.frame_parser.view.dialog.button.cancel}', submit:'{lang admin.ilinei.frame_parser.view.dialog.button.save}'}, submit:function(){
            $('#frm_grid textarea').remove();

            $('.-ilinei-block').each(function(index, item){
                $('> textarea', item).val(tag_get_textarea(item));
                $('#frm_grid').append($('> textarea', item));
            });

            //$('#frm_grid').submit();

            $.dialog({message:'<p class="-ilinei-tips">美好的事情即将发生！</p>'});
        }});
    }

    //顶部添加TAG
    function block_add(){
        $.dialog({height:120, title:'{lang admin.ilinei.frame_parser.view.block.tag}', message:messages['type'], button:{cancel:'{lang admin.ilinei.frame_parser.view.dialog.button.cancel}', submit:'{lang admin.ilinei.frame_parser.view.dialog.button.add}'}, submit:function(){
            var block_type = $("#_dialog .-ilinei-type input[name='rdoType']:checked").val();

            //prev顺序为：3-2-1；next顺序为：1-2-3
            if(page_type == 'page'){
                if(block_type == 'prev'){
                    tag_loops['body_prev']++;
                    $('body').prepend(tag_get_tpl(0, 'body_prev', 'body_prev[' + tag_loops['body_prev'] + '][0]'));
                }else{
                    tag_loops['body_next']++;
                    $('body').append(tag_get_tpl(0, 'body_next', 'body_next[' + tag_loops['body_next'] + '][0]'));
                }
            }else{
                if(block_type == 'prev'){
                    tag_loops['body_prev']++;
                    $(tag_get_tpl(0, 'body_prev', 'body_prev[' + tag_loops['body_prev'] + '][0]')).insertBefore('.-ilinei-block-prev');
                }else{
                    tag_loops['body_next']++;
                    $(tag_get_tpl(0, 'body_next', 'body_next[' + tag_loops['body_next'] + '][0]')).insertBefore('.-ilinei-block-next');
                }
            }

            $.dialog({close:true});

            block_bind_event();
        }});
    }

    //绑定TAG事件
    function block_bind_event(){
        //在编辑小图标后加上名称
        $('.-ilinei-block').each(function(index, item){
            //TAG的名称
            var tag_identity = tag_get_identity(item);
            $('.-ilinei-icon-text', item).text(tag_names[tag_identity]);
        });

        //TAG鼠标经过
        $('.-ilinei-block').unbind('hover').hover(function(){
            $(this).addClass('-ilinei-block-hover');
        }, function(){
            $(this).removeClass('-ilinei-block-hover');
        });

        //TAG点击编辑小图标
        $('.-ilinei-block .-ilinei-icon-img').unbind('click').click(function(){
            var tag = $(this).parent().parent();
            var tag_parent = $(tag).parent();

            var tag_name = $(tag).attr('tag');
            var tag_file = $(tag).attr('file');
            var tag_model = $(tag).attr('model');
            var tag_method = $(tag).attr('method');
            var tag_level = $(tag).attr('level') - 0;
            var tag_rel = $(tag).attr('rel');

            var tag_identity = tag_get_identity(tag);

            var message = messages['operate'] + $('#txt_tag_list').val();

            $.dialog({width:700, height:300, title:tag_name + '{lang admin.ilinei.frame_parser.view.block.tag}', message:message, button:{cancel:'{lang admin.ilinei.frame_parser.view.dialog.button.cancel}', submit:'{lang admin.ilinei.frame_parser.view.dialog.button.submit}'}, callback:function(){
                //空TAG只能删除
                if(tag_identity == 'blank'){
                    $('#_dialog .-ilinei-operate .out').remove();
                    $('#_dialog .-ilinei-operate .in').remove();
                }else if(tag_identity == 'block' || tag_identity == 'ad_model__adlog'){
                    $('#_dialog .-ilinei-operate .in').remove();
                }

                //只支持1个内部前TAG
                if($('> .-ilinei-block > .textarea-prev', tag).length > 0){
                    $('#_dialog .-ilinei-operate .btn-add-interval-prev').remove();
                }

                //只支持1个内部后TAG
                if($('> .-ilinei-block > .textarea-next', tag).length > 0){
                    $('#_dialog .-ilinei-operate .btn-add-interval-next').remove();
                }

                $('#_dialog .-ilinei-tag .btn').removeClass('btn-choice');

                if(tag_identity == 'cms_model__category_multi'){
                    $('#_dialog .-ilinei-tag .tag-cms_model__category_multi').addClass('btn-choice');

                    $('#tag-cms_model__category_multi').show();
                    $("#tag-cms_model__category_multi input[name='txtIdentity']").val($(tag).attr('identity') ? $(tag).attr('identity') : '');
                    $("#tag-cms_model__category_multi input[name='txtVar']").val($(tag).attr('var') ? $(tag).attr('var') : '');
                }else if(tag_identity == 'cms_model__category_crumbs'){
                    $('#_dialog .-ilinei-tag .tag-cms_model__category_crumbs').addClass('btn-choice');

                    $('#tag-cms_model__category_crumbs').show();
                    $("#tag-cms_model__category_crumbs input[name='txtIdentity']").val($(tag).attr('identity') ? $(tag).attr('identity') : '');
                    $("#tag-cms_model__category_crumbs input[name='txtVar']").val($(tag).attr('var') ? $(tag).attr('var') : '');
                }else if(tag_identity == 'cms_model__category_one'){
                    $('#_dialog .-ilinei-tag .tag-cms_model__category_one').addClass('btn-choice');

                    $('#tag-cms_model__category_one').show();
                    $("#tag-cms_model__category_one input[name='txtParam']").val($(tag).attr('param') ? $(tag).attr('param') : '');
                    $("#tag-cms_model__category_one input[name='txtType']").val($(tag).attr('type') ? $(tag).attr('type') : '');
                    $("#tag-cms_model__category_one input[name='txtVar']").val($(tag).attr('var') ? $(tag).attr('var') : '');
                }else if(tag_identity == 'cms_model__article_multi'){
                    $('#_dialog .-ilinei-tag .tag-cms_model__article_multi').addClass('btn-choice');

                    $('#tag-cms_model__article_multi').show();
                    $("#tag-cms_model__article_multi input[name='txtIdentity']").val($(tag).attr('identity') ? $(tag).attr('identity') : '');
                    $("#tag-cms_model__article_multi input[name='txtParam']").val($(tag).attr('param') ? $(tag).attr('param') : '');
                    $("#tag-cms_model__article_multi input[name='txtLimit']").val($(tag).attr('limit') ? $(tag).attr('limit') : '');

                    if($(tag).attr('pager') == 'true') $("#tag-cms_model__category_one input[name='cbxPager']").attr('checked', true);
                    else $("#tag-cms_model__category_one input[name='cbxPager']").removeAttr('checked');

                    $("#tag-cms_model__article_multi input[name='txtVar']").val($(tag).attr('var') ? $(tag).attr('var') : '');
                }else if(tag_identity == 'cms_model__article_one'){
                    $('#_dialog .-ilinei-tag .tag-cms_model__article_one').addClass('btn-choice');

                    $('#tag-cms_model__article_one').show();
                    $("#tag-cms_model__article_one input[name='txtParam']").val($(tag).attr('param') ? $(tag).attr('param') : '');
                    $("#tag-cms_model__article_one input[name='txtVar']").val($(tag).attr('var') ? $(tag).attr('var') : '');
                }else if(tag_identity == 'block'){
                    $('#_dialog .-ilinei-tag .tag-block').addClass('btn-choice');
                    $('#tag-block').show();

                    $("#tag-block input[name='rdoBlock']").removeAttr('checked');
                    $("#tag-block input[name='rdoBlock'][value='" + tag_file + "']").attr("checked",true);
                }else if(tag_identity == 'blank'){
                    $('#_dialog .-ilinei-tag .tag-blank').addClass('btn-choice');

                    $('#tag-blank').show();
                }else if(tag_identity == 'ad_model__adlog'){
                    $('#_dialog .-ilinei-tag .tag-ad_model__adlog').addClass('btn-choice');

                    $('#tag-ad_model__adlog').show();
                    $("#tag-ad_model__adlog input[name='txtIdentity']").val($(tag).attr('identity') ? $(tag).attr('identity') : '');
                }

                $('#_dialog .-ilinei-tag .btn').click(function(){
                    $('#_dialog .-ilinei-tag .btn').removeClass('btn-choice');
                    $(this).addClass('btn-choice');

                    $('#_dialog .tag').hide();
                    $('#tag-' + $(this).attr('rel')).show();
                });

                //TAG前添加空TAG
                $('#_dialog .-ilinei-operate .btn-add-prev').click(function() {
                    if(tag_identity == 'blank'){
                        $.dialog({message:'<p class="-ilinei-tips">{lang admin.ilinei.frame_parser.view.dialog.message.level}</p>'});
                        return;
                    }

                    tag_loops[tag_rel + '_prev']++;

                    //当前TAG的文本域名称
                    //顺序为：1-2-3
                    var textarea = tag_rel + '_prev[' + tag_loops[tag_rel + '_prev'] + '][0]';
                    $(tag_get_tpl(tag_level, tag_rel, textarea)).insertBefore(tag);
                    block_bind_event();

                    $.dialog({close:true});
                });

                //TAG后添加空TAG
                $('#_dialog .-ilinei-operate .btn-add-next').click(function(){
                    if(tag_identity == 'blank'){
                        $.dialog({message:'<p class="-ilinei-tips">{lang admin.ilinei.frame_parser.view.dialog.message.level}</p>'});
                        return;
                    }

                    tag_loops[tag_rel + '_next']++;

                    //当前TAG的文本域名称
                    //顺序为：3-2-1
                    var textarea = tag_rel + '_next[' + tag_loops[tag_rel + '_next'] + '][0]';
                    $(tag_get_tpl(tag_level, tag_rel, textarea)).insertAfter(tag);
                    block_bind_event();

                    $.dialog({close:true});
                });

                //TAG内头部添加空TAG
                $('#_dialog .-ilinei-operate .btn-add-interval-prev').click(function(){
                    if(tag_level >= 1 || tag_identity == 'blank'){
                        $.dialog({message:'<p class="-ilinei-tips">{lang admin.ilinei.frame_parser.view.dialog.message.level}</p>'});
                        return;
                    }

                    //当前TAG的文本域名称
                    var textarea = $('textarea', tag).attr('name');
                    textarea = textarea.substr(0, textarea.length - 3);
                    textarea += '[1]';

                    $(tag_get_tpl(tag_level + 1, tag_rel, textarea, 'textarea-prev')).insertBefore($('> .-ilinei-block-content', tag));
                    block_bind_event();

                    $.dialog({close:true});
                });

                //TAG内底部添加空TAG
                $('#_dialog .-ilinei-operate .btn-add-interval-next').click(function(){
                    if(tag_level >= 1 || tag_identity == 'blank'){
                        $.dialog({message:'<p class="-ilinei-tips">{lang admin.ilinei.frame_parser.view.dialog.message.level}</p>'});
                        return;
                    }

                    //当前TAG的文本域名称
                    var textarea = $('textarea', tag).attr('name');
                    textarea = textarea.substr(0, textarea.length - 3);
                    textarea += '[2]';

                    $(tag_get_tpl(tag_level + 1, tag_rel, textarea, 'textarea-next')).insertAfter($('> .-ilinei-block-content', tag));
                    block_bind_event();

                    $.dialog({close:true});
                });

                //TAG删除
                $('#_dialog .-ilinei-operate .btn-delete').click(function(){
                    $.dialog({message:'<p class="-ilinei-tips">{lang admin.ilinei.frame_parser.view.dialog.message.delete}</p>', button:{cancel:'{lang admin.ilinei.frame_parser.view.dialog.button.cancel}', submit:'{lang admin.ilinei.frame_parser.view.dialog.button.delete}'}, submit:function(){
                        $(tag).remove();
                    }});
                });
            }, submit:function(){
                var rel = $('#_dialog .-ilinei-tag .btn-choice').attr('rel');

                $(tag).removeAttr('file');
                $(tag).attr('tag', 'block');

                if(rel == 'cms_model__category_multi'){
                    $(tag).attr('model', '\\cms\\model\\_category');
                    $(tag).attr('method', 'multi');
                    $(tag).attr('identity', $("#tag-cms_model__category_multi input[name='txtIdentity']").val());
                    $(tag).attr('var', $("#tag-cms_model__category_multi input[name='txtVar']").val());
                }else if(rel == 'cms_model__category_crumbs'){
                    $(tag).attr('model', '\\cms\\model\\_category');
                    $(tag).attr('method', 'crumbs');
                    $(tag).attr('identity', $("#tag-cms_model__category_crumbs input[name='txtIdentity']").val());
                    $(tag).attr('var', $("#tag-cms_model__category_crumbs input[name='txtVar']").val());
                }else if(rel == 'cms_model__category_one'){
                    $(tag).attr('model', '\\cms\\model\\_category');
                    $(tag).attr('method', 'one');
                    $(tag).attr('param', $("#tag-cms_model__category_one input[name='txtParam']").val());
                    $(tag).attr('type', $("#tag-cms_model__category_one input[name='txtType']").val());
                    $(tag).attr('var', $("#tag-cms_model__category_one input[name='txtVar']").val());
                }else if(rel == 'cms_model__article_multi'){
                    $(tag).attr('model', '\\cms\\model\\_article');
                    $(tag).attr('method', 'multi');
                    $(tag).attr('identity', $("#tag-cms_model__article_multi input[name='txtIdentity']").val());
                    $(tag).attr('param', $("#tag-cms_model__article_multi input[name='txtParam']").val());
                    $(tag).attr('limit', $("#tag-cms_model__article_multi input[name='txtLimit']").val());

                    if($("#tag-cms_model__article_multi input[name='cbxPager']:checked").length) $(tag).attr('pager', 'true');
                    else $(tag).removeAttr('pager');

                    $(tag).attr('var', $("#tag-cms_model__article_multi input[name='txtVar']").val());
                }else if(rel == 'cms_model__article_one'){
                    $(tag).attr('model', '\\cms\\model\\_article');
                    $(tag).attr('method', 'one');

                    $(tag).attr('param', $("#tag-cms_model__article_one input[name='txtParam']").val());
                    $(tag).attr('var', $("#tag-cms_model__article_one input[name='txtVar']").val());
                }else if(rel == 'block'){
                    $(tag).attr('file', $("#tag-block input[name='rdoBlock']:checked").val());
                }else if(rel == 'ad_model__adlog'){
                    $(tag).attr('tag', 'adlog');
                    $(tag).attr('identity', $("#tag-ad_model__adlog input[name='txtIdentity']").val());
                }else if(rel == 'blank'){
                    $(tag).attr('file', '');
                }

                var ref = $(tag).attr('ref');
                var identity = tag_get_identity(tag);

                ref = ref.trim();
                identity = identity.trim();

                if(ref == identity) $(tag).removeAttr('change');
                else $(tag).attr('change', 'true');

                //嗯，能看到这块说明你很了不起。
                var tag_content = $(tag).prop("outerHTML");

                $.post('{$ADMIN_SCRIPT}/admin/file/_parse&file={$_var[gp_file]}&path={$_var[gp_path]}&rnd=' + Math.random(), {content: tag_content}, function(data){
                    //没有什么意思，只是更酷一些
                    setTimeout(function(){
                        $('.-ilinei-block-content', tag).html(data);

                        block_bind_event();
                    }, 200);
                });
            }});
        });
    }

    function tag_get_tpl(level, rel, textarea){
        var tag_tpl = '';

        tag_tpl += "<div class=\"-ilinei-block\" tag=\"blank\" level=\"" + level + "\" rel=\"" + rel + "\" ref=\"blank\">";
        tag_tpl += "<textarea name=\"" + textarea + "\" " + (arguments.length > 3 ? "class=\"" + arguments[3] + "\"" : '') + " style=\"display:none;\"></textarea>";
        tag_tpl += "<p class=\"-ilinei-icon\">";
        tag_tpl += "<span class=\"-ilinei-icon-img\"></span>";
        tag_tpl += "<span class=\"-ilinei-icon-text\"></span>";
        tag_tpl += "</p>";
        tag_tpl += "<div class=\"-ilinei-block-content\"></div>";
        tag_tpl += "</div>";

        return tag_tpl;
    }

    function tag_get_identity(tag){
        var tag_name = $(tag).attr('tag');
        var tag_file = $(tag).attr('file');
        var tag_model = $(tag).attr('model');
        var tag_method = $(tag).attr('method');

        if(tag_name == 'block'){
            if(typeof(tag_file) == 'undefined'){
                //没有定义file
                if(tag_model == '\\cms\\model\\_category'){
                    if(tag_method == 'multi') return 'cms_model__category_multi';
                    else if(tag_method == 'crumbs') return 'cms_model__category_crumbs';
                    else if(tag_method == 'one') return 'cms_model__category_one';
                }else if(tag_model == '\\cms\\model\\_article'){
                    if(tag_method == 'multi') return 'cms_model__article_multi';
                    else if(tag_method == 'one') return 'cms_model__article_one';
                }
            }else if(tag_file == '') return 'blank';
            else return 'block';
        }else if(tag_name == 'adlog') return 'ad_model__adlog';

        return 'blank';
    }

    function tag_get_textarea(tag){
        var textarea = '';

        var ref = $(tag).attr('ref');
        var identity = tag_get_identity(tag);

        ref = ref.trim();
        identity = identity.trim();

        var tag_change = ref == identity ? '' : " change=\"true\"";

        if(identity == 'ad_model__adlog'){
            textarea = "ADLOG" + tag_change + " " + $(tag).attr('identity');
        }else if(identity == 'blank'){
            textarea = "BLOCK" + tag_change + " file=\"\"";
        }else if(identity == 'block'){
            textarea = "BLOCK" + tag_change + " file=\"" + $(tag).attr('file') + "\"";
        }else if(identity == 'cms_model__category_multi'){
            textarea = "BLOCK" + tag_change + " model=\"\\cms\\model\\_category\" method=\"multi\" identity=\"" + $(tag).attr('identity') + "\" var=\"" + $(tag).attr('var') + "\"";
        }else if(identity == 'cms_model__category_crumbs'){
            textarea = "BLOCK" + tag_change + " model=\"\\cms\\model\\_category\" method=\"crumbs\" identity=\"" + $(tag).attr('identity') + "\" var=\"" + $(tag).attr('var') + "\"";
        }else if(identity == 'cms_model__category_one'){
            textarea = "BLOCK" + tag_change + " model=\"\\cms\\model\\_category\" method=\"one\" param=\"" + $(tag).attr('param') + "\" type=\"" + $(tag).attr('type') + "\" var=\"" + $(tag).attr('var') + "\"";
        }else if(identity == 'cms_model__article_multi'){
            textarea = "BLOCK" + tag_change + " model=\"\\cms\\model\\_article\" method=\"multi\" identity=\"" + $(tag).attr('identity') + "\" param=\"" + $(tag).attr('param') + "\" limit=\"" + $(tag).attr('limit') + "\" ";
            if($(tag).attr('pager')) textarea += "pager=\"" + $(tag).attr('pager') + "\" ";
            textarea += "var=\"" + $(tag).attr('var') + "\"";
        }else if(identity == 'cms_model__article_one'){
            textarea = "BLOCK" + tag_change + " model=\"\\cms\\model\\_article\" method=\"one\" param=\"" + $(tag).attr('param') + "\" var=\"" + $(tag).attr('var') + "\"";
        }else textarea = "BLOCK" + tag_change + " file=\"\"";

        return textarea;
    }

    $(function(){
        //首次加载好，生成TAG的序号缓存
        $('.-ilinei-block').each(function(index, item){
            //文本域序号数组
            var rel = $(item).attr('rel');
            if(rel){
                tag_loops[rel + '_prev'] = 0;
                tag_loops[rel + '_next'] = 0;
            }

            var identity = tag_get_identity(item);
            identity = identity.trim();

            $(item).attr('ref', identity);
        });

        block_bind_event();
    });
</script>